package com.majiang.majiang.service;

import com.majiang.majiang.domain.Player;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author Administrator
 * 初始化玩家：创建四个玩家（包括3个电脑）
 */
@Setter
@Getter
@ToString
public class InitPlayer {
    private List<Player> players = new ArrayList<>();
    private Player player = null;
    private Player computer1 = null;
    private Player computer2 = null;
    private Player computer3 = null;

    public InitPlayer() {
        //创建4个玩家
        createPlayer();
    }

    /**
     * 创建4个玩家
     */
    private void createPlayer(){
        player = new Player("玩家");
        computer1 = new Player("电脑1");
        computer2 = new Player("电脑2");
        computer3 = new Player("电脑3");
        players.add(player);
        players.add(computer1);
        players.add(computer2);
        players.add(computer3);
    }

    public Player getCurrentPlayer(){
        return players.get(0);
    }

    /**
     * 输出四个玩家手中的牌
     */
    public void printPlayer(){
        //输出当前玩家手中的牌
        System.out.println("【当前玩家手中的牌如下：】");
        System.out.print("【玩家】");
        player.printMaJiangs();
        System.out.print("\n【电脑1】");
        computer1.printMaJiangs();
        System.out.print("\n【电脑2】");
        computer2.printMaJiangs();
        System.out.print("\n【电脑3】");
        computer3.printMaJiangs();
        System.out.println();
    }

    /**
     * 第一次起牌：轮流起牌，每次起4张，一共起3轮
     */
    public void haveFirstBoard(ShuffleMaJiang shuffleMaJiang){
        //共3轮
        for (int i = 0; i < 3; i++) {
            //玩家，每次起4张
            for (int j = 0; j < 4; j++) {
                player.gainMaJiang(shuffleMaJiang, shuffleMaJiang.getMaJiangsIndex());
            }
            //电脑1，每次起4张
            for (int j = 0; j < 4; j++) {
                computer1.gainMaJiang(shuffleMaJiang, shuffleMaJiang.getMaJiangsIndex());
            }
            //电脑2，每次起4张
            for (int j = 0; j < 4; j++) {
                computer2.gainMaJiang(shuffleMaJiang, shuffleMaJiang.getMaJiangsIndex());
            }
            //电脑3，每次起4张
            for (int j = 0; j < 4; j++) {
                computer3.gainMaJiang(shuffleMaJiang, shuffleMaJiang.getMaJiangsIndex());
            }
        }
    }

    /**
     * 第二次起牌：跳庄（玩家拿第1和第5张，电脑1拿第2张，电脑2拿第3张，电脑3拿第4张）
     */
    public void haveJumpBoard(ShuffleMaJiang shuffleMaJiang){
        player.gainMaJiang(shuffleMaJiang, 0);
        player.gainMaJiang(shuffleMaJiang, 4);
        computer1.gainMaJiang(shuffleMaJiang, 1);
        computer2.gainMaJiang(shuffleMaJiang, 2);
        computer3.gainMaJiang(shuffleMaJiang, 3);
    }

    /**
     * 翻癞子（癞子可以当成是任何一张牌）
     */
    public void haveWildBoard(ShuffleMaJiang shuffleMaJiang){
        System.out.println("【癞子是：" + shuffleMaJiang.getMaJiangs().get(5) + "】");
        //直接移除第6张牌
        shuffleMaJiang.getMaJiangs().remove(5);
    }

    /**
     * 顺序输出牌
     */
    public void sort() {
        player.sort();
        computer1.sort();
        computer2.sort();
        computer3.sort();
    }

    public boolean touchAndBar(Scanner scanner, ShuffleMaJiang shuffleMaJiang)  throws InterruptedException{
        System.out.println("\n【河里牌：】");
        shuffleMaJiang.printMaJiangsRiver();
        int i = 0;
        boolean tauchOrBar = false;
        Player playerTouch = null;
        for(Player player: players){
            if(i++ != 3 && player.getName().equals("玩家")){
                if(player.isMingBarCards(shuffleMaJiang)){
                    System.out.println("【等待" + player.getName() + "操作：请输入:[1:杠牌]，[2:不做操作],[其他输入:退出游戏]】");
                    String inputOperate = scanner.nextLine();
                    if ("1".equals(inputOperate)) {
                        player.mingBarCards(shuffleMaJiang);
                    }else if("2".equals(inputOperate)) {
                        continue;
                    }else{
                        shuffleMaJiang.huEnd();
                    }
                    tauchOrBar = true;
                    playerTouch = player;
                }
                if(player.isTouchCards(shuffleMaJiang)){
                    System.out.println("【等待" + player.getName() + "操作：请输入:[1:碰牌]，[2:不做操作],[其他输入:退出游戏]】");
                    String inputOperate = scanner.nextLine();
                    if ("1".equals(inputOperate)) {
                        player.touchCards(shuffleMaJiang);
                    }else if("2".equals(inputOperate)) {
                        continue;
                    }else{
                        shuffleMaJiang.huEnd();
                    }
                    tauchOrBar = true;
                    playerTouch = player;
                }
            }else{
                //先开杠
                if(player.isMingBarCards(shuffleMaJiang)){
                    player.mingBarCards(shuffleMaJiang);
                    tauchOrBar = true;
                    playerTouch = player;
                }
                //再碰
                if(player.isTouchCards(shuffleMaJiang)){
                    player.touchCards(shuffleMaJiang);
                    tauchOrBar = true;
                    playerTouch = player;
                }
            }
        }
        if(tauchOrBar){
            recomputeNextPlayer(playerTouch);

        }
        return tauchOrBar;
    }

    private void recomputeNextPlayer(Player player) {
        List<Player> newList = new ArrayList<>();
        int index = players.indexOf(player);
        for(int i = 0; i < players.size(); i++){
            newList.add(players.get(index++));
            if(index >= players.size()){
                index = 0;
            }
        }
        players = newList;
    }

    public void recomputeNextPlayer() {
        List<Player> newList = new ArrayList<>();
        newList.add(players.get(1));
        newList.add(players.get(2));
        newList.add(players.get(3));
        newList.add(players.get(0));
        players = newList;
    }

}
